package com.ad.pet.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.ad.pet.Logger;
import com.ad.pet.R;
import com.ad.pet.common.data.utils.ui.ToastUtil;
import com.ad.pet.common.structure.events.Events;
import com.ad.pet.common.structure.events.RxBus;
import com.ad.pet.constant.Constant;
import com.google.gson.Gson;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelpay.PayResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by SEELE on 2018/4/8.
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Constant.APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        PayResp baseResp = (PayResp) resp;
        Logger.e("baseResp",baseResp.extData+"|"+baseResp.errCode+"|"+baseResp.errStr);

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {


//            String url = "";


            switch (resp.errCode) {
                case 0://支付成功
                    String respUrl ="";
                        respUrl= new Gson().toJson(resp);
                    RxBus.getDefault().post("confim_success");
                    RxBus.getDefault().post(new Events.PayStatusCallback(baseResp.extData,"weixin_app",respUrl));

                    finish();
                    break;
                case -1://支付失败
                    ToastUtil.getToastUtil().showShort(getString(R.string.pay_failed));
                    finish();
                    break;
                case -2://取消支付
                    ToastUtil.getToastUtil().showShort(getString(R.string.cancel));
                    finish();
                    break;
            }

        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}